<?php  namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Request;

class Base extends Api {
    public $userinfo;
    protected $noNeedLogin = ['checktoken'];
    public $site;
    public function __construct()
    {
        parent::__construct();
        $this->checktoken();
        $this->setSite();
    }
    public function setSite(){
        $this->site = \think\Config::get('site');
    }
    /**
     *  检查tokens 是否合法
     */
    public function checktoken(){
        $token =  $this->request->header('token');
        if (!$this->auth->match($this->noNeedLogin)) {
            if (!$token) {
                $this->error("非法访问");
            }
            $tokenInfo = \app\common\library\Token::get($token);

            if(empty($tokenInfo["token"])){
                $this->error("token不存在");
            }
            //TOKEN 是否合法
            if($tokenInfo['token']!=$token){
                $this->error("token 不合法 或不存在");
            }
            //TOKEN 是否过期

            $this->userinfo = $this->auth->getUserinfo();
        }

    }

}